package com.scfire.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;

import com.mongodb.*;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import com.scfire.weixin.WeiXinConstants;

/**
 * 上传，下载文件保存到本地服务器硬盘上
 * 
 * @author Coca
 *
 */
public class FileUtil {
	public static String imgSave(byte[] bytes, String uuid) {
		try {
			File filePath = new File(WeiXinConstants.imgDir);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}

			if (bytes != null) {
				FileOutputStream fo = new FileOutputStream(
						WeiXinConstants.imgDir + uuid.toString());
				fo.write(bytes);
				fo.flush();
				fo.close();
				return uuid.toString();
			}
		} catch (Exception e) {
			System.out.println("文件保存异常" + e.toString());
		}
		return null;
	}

	public static byte[] imgRead(String name) {
		try {
			File file = new File(WeiXinConstants.imgDir + name);
			if (file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				byte[] b = new byte[1000];
				int n;
				while ((n = fis.read(b)) != -1) {
					bos.write(b, 0, n);
				}
				fis.close();
				bos.close();
				return bos.toByteArray();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static boolean fileCopy(String remoteFilePath, String localFilePath) {
		URL urlfile = null;
		HttpURLConnection httpUrl = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		File f = new File(localFilePath);
		try {	
			urlfile = new URL(remoteFilePath);
			httpUrl = (HttpURLConnection) urlfile.openConnection();
			httpUrl.connect();
			bis = new BufferedInputStream(httpUrl.getInputStream());
			bos = new BufferedOutputStream(new FileOutputStream(f));
			int len = 2048;
			byte[] b = new byte[len];
			while ((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			bos.flush();
			bis.close();
			httpUrl.disconnect();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				bis.close();
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
	}

	/**
	 * 批量导出图片到文件夹 public static void exportImgToDir() throws IOException,
	 * NoSuchFieldException, SecurityException, IllegalArgumentException,
	 * IllegalAccessException{ Mongo mongo =new Mongo("121.40.62.73", 27017); DB
	 * db = mongo.getDB("SCFIREDevDB");
	 * 
	 * GridFS gridFS = new GridFS(db); DBCursor sor = gridFS.getFileList();
	 * Iterator<DBObject> it = sor.iterator();
	 * 
	 * Field _fs = GridFSFile.class.getDeclaredField("_fs"); //
	 * _fs字段所在的类为GridFSFile _fs.setAccessible(true); int i = 1;
	 * while(it.hasNext()){ ByteArrayOutputStream byteArrayOutputStream = new
	 * ByteArrayOutputStream(); GridFSDBFile file = (GridFSDBFile)it.next();
	 * _fs.set(file, gridFS); String fileName = file.getFilename(); String
	 * contentType = file.getContentType(); String id = file.getId().toString();
	 * file.writeTo(byteArrayOutputStream);
	 * 
	 * imgSave(byteArrayOutputStream.toByteArray() , id);
	 * System.out.print(i+"  "); byteArrayOutputStream.close(); i++; }
	 * System.out.println("导出完成"); }
	 */

}
